import { useRef, useCallback, useEffect } from 'react'

// 用于持久化函数
function usePersistFn(fn) {
  let ref = useRef(function() {
    throw new Error('Cannot call an event handler while rendering. :)')
  })
  useEffect(
    function() {
      ref.current = fn
    },
    [fn]
  )
  let persistedFn = useCallback(function() {
    return ref.current.apply(ref, arguments)
  }, [])
  return persistedFn
}

export default usePersistFn
